package com.scau.ums.service.imp.suggest;

import com.scau.ums.mapper.CustomSuggestionMapper;
import com.scau.ums.mapper.SuggestionMapper;
import com.scau.ums.mapper.TeacherMapper;
import com.scau.ums.model.Suggestion;
import com.scau.ums.model.SuggestionExample;
import com.scau.ums.model.Teacher;
import com.scau.ums.service.CommonSuggest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * author: 莫伟杰
 * date： 2019/7/23 14:44
 */
@Service
public class TeacherSuggestImp implements CommonSuggest {

    private TeacherMapper teacherMapper;
    public SuggestionMapper suggestionMapper;
    private CustomSuggestionMapper customSuggestionMapper;

    @Autowired
    public TeacherSuggestImp(TeacherMapper teacherMapper,SuggestionMapper suggestionMapper,CustomSuggestionMapper customSuggestionMapper)
    {
        this.teacherMapper = teacherMapper;
        this.suggestionMapper = suggestionMapper;
        this.customSuggestionMapper = customSuggestionMapper;
    }

    @Override
    public List<Suggestion> getAllSuggestions() {
        SuggestionExample example = new SuggestionExample();
        example.createCriteria().andStateEqualTo(STATE_PASS);
        return suggestionMapper.selectByExample(example);
    }//获取所有通过审核的言论

    @Override
    public List<Suggestion> getUserSuggestions(String userID) {
        SuggestionExample example = new SuggestionExample();
        example.createCriteria().andSpeakeridEqualTo(userID);
        return suggestionMapper.selectByExample(example);
    }

    //获取最新n条记录,startIndex=开始下标（从0开始,含下标）,pageSize=记录条数
    //普通用户只能获取通过审核的记录
    @Override
    public List<Suggestion> getLatestSuggestions(int startIndex, int pageSize) {
        String whereClause = "state = '"+STATE_PASS+"'";
        return customSuggestionMapper.selectRowByPageSetOrderByClause
                ("suggestion","date DESC",startIndex,pageSize,whereClause);
    }

    @Override
    public boolean newSuggestions(String userID, String suggestionStr) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(userID);
        if(teacher==null) return false;
        Suggestion suggestion = new Suggestion();
        suggestion.setId(0);
        suggestion.setSpeakername(teacher.getName());
        suggestion.setSpeakerid(teacher.getId());
        suggestion.setContent(suggestionStr);
        suggestion.setDate(new Date());
        suggestion.setSpeakertype("老师");
        suggestion.setState(STATE_VERIFY);
        suggestion.setIstop(ISTOP_FALSE);
        return suggestionMapper.insert(suggestion)==1;
        //判断受影响行数return false;
    }

    @Override
    public boolean delSuggestion(Suggestion s) {
        return suggestionMapper.deleteByPrimaryKey(s.getId())==1;
    }
}
